package com.sp.controller;

import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 提供RESTful API的控制器
 */
@RestController
@RequestMapping("/api")
public class ApiController {

    /**
     * 简单的GET请求示例
     */
    @GetMapping("/greet")
    public String greet(@RequestParam(required = false, defaultValue = "World") String name) {
        return "Hello, " + name + "!";
    }

    /**
     * 获取用户信息的示例
     */
    @GetMapping("/user/{id}")
    public Map<String, Object> getUser(@PathVariable Long id) {
        Map<String, Object> user = new HashMap<>();
        user.put("id", id);
        user.put("name", "John Doe");
        user.put("email", "john.doe@example.com");
        return user;
    }

    /**
     * 处理POST请求的示例
     */
    @PostMapping("/echo")
    public Map<String, String> echo(@RequestBody Map<String, String> request) {
        Map<String, String> response = new HashMap<>();
        response.put("status", "success");
        response.put("message", "Received: " + request.get("message"));
        return response;
    }
}
